package com.superme.freemarkerquickstart.controller;

import org.springframework.stereotype.Controller;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class BaseController {

    /*
  设置返回的RESTAPI结果中日期的格式
   */
    protected ArrayList<HashMap<String, Object>> SetRowsDateFormat(HashMap obj, String DateFieldName, String DateFormat) {
        ArrayList<HashMap<String, Object>> rows = null;
        if (obj.get("content") != null) {
            rows = (ArrayList<HashMap<String, Object>>) obj.get("content");
            for (HashMap<String, Object> row : rows) {
                if (row.get(DateFieldName) != null) {
                    Long lBirthday = (Long) row.get(DateFieldName);
                    Date d = new Date(lBirthday);
                    java.text.DateFormat format = new SimpleDateFormat(DateFormat);
                    String strBirthday = format.format(d);
                    row.put(DateFieldName, strBirthday);
                }
            }

        }
        return rows;
    }

    //将传入的map信息转成相应的对象
    protected Object mapToTableConfigObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null)
            return null;
        Object obj = beanClass.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                //从string转成相应的字段类型
                //主要是因为queryparam传入的都是字符串所以要进行类型转换
                if(property.getPropertyType() == Integer.class)
                {
                    Integer object;
                    object = Integer.valueOf(map.get(property.getName()).toString());
                    map.put(property.getName(),object);
                }
                setter.invoke(obj, map.get(property.getName()));
            }
        }

        return obj;
    }

    //将对象转换成map
    protected  Map<?, ?> tableConfigtObjectToMap(Object obj) throws Exception {
        if(obj == null)
            return null;
        Map<String, Object> map = new HashMap<String, Object>();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter!=null ? getter.invoke(obj) : null;
            map.put(key, value);
        }
        return map;
    }
}
